package org.electric.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.electric.bean.productBean;
import org.electric.util.JDBCUtil;
import org.electric.util.Page;



public class ProductDao {
	
	
/*
 * 创建产品	
 */
 public int addProduct(productBean p){
	 Connection conn=JDBCUtil.getConection();
	 StringBuffer sql=new StringBuffer();
	 sql.append("INSERT INTO `product_info` (product_id,product_name,product_desc,license_id,image,status)");
	 sql.append("VALUES(?,?,?,?,?,1)");
	 PreparedStatement ptmt=null;
	
	 int count=0;
	    try {
	    	ptmt=conn.prepareStatement(sql.toString());
			ptmt.setObject(1, p.getProduct_id());
			ptmt.setObject(2, p.getProduct_name());
			ptmt.setObject(3, p.getProduct_desc());
			ptmt.setObject(4, p.getLicense_id());
			ptmt.setObject(5, p.getImage());
					
			count=ptmt.executeUpdate();
			
			
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 
		JDBCUtil.close(conn, ptmt);
	return count; 
 }	
	
	

	/*	
	 * 查询我的所有产品的基本信息/展示
	 */
		public List<productBean> myProductAll(productBean p,Page pa){
			p.setLicense_id("AE86");
			
			 Connection conn=JDBCUtil.getConection();
		   List<productBean> list =new ArrayList<productBean>();
		   StringBuffer sql=new StringBuffer();
		   sql.append("SELECT * FROM product_info WHERE license_id=? AND status=1 LIMIT ?,?");
		   PreparedStatement stmt =null;
		   ResultSet rs=null;
		   try {
			stmt = conn.prepareStatement(sql.toString());
		    stmt.setString(1, p.getLicense_id());
		    stmt.setInt(2, pa.getIndexNo()); 
		    stmt.setInt(3, pa.getPageSize()); 
		   
			 rs=stmt.executeQuery();
			 while(rs.next()){
			 productBean pr=new productBean();
				 pr.setProduct_id(rs.getString(2));
				 pr.setProduct_name(rs.getString(3));
				 pr.setProduct_desc(rs.getString(4));
			     pr.setLicense_id(rs.getString(5));
			     pr.setImage(rs.getString(6));
		       
			     list.add(pr);
			 }
			 
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
	 
			JDBCUtil.close(conn, stmt,rs);
		  return list;
	 
	   }
	
	
	
	/*	
	 * 修改my产品信息
	 */
   public int UpProduct(productBean p){
	   Connection conn=JDBCUtil.getConection();
	   int count=0;
	   StringBuffer sql=new StringBuffer();
	   sql.append("UPDATE product_info SET ");
	   sql.append("product_id=?");
	   sql.append("product_name=?");
	   sql.append("product_desc=?");
	   sql.append("Image=?");
	   sql.append("WHERE  product_id ='"+p.getProduct_id()+"'");
	   PreparedStatement stat=null;
	   System.out.println(sql);
	   try {
		stat=conn.prepareStatement(sql.toString());
		
		stat.setObject(1, p.getProduct_id());
		stat.setObject(2, p.getProduct_name());
		stat.setObject(3, p.getProduct_desc());
		stat.setObject(4, p.getImage());
		
		count=stat.executeUpdate(sql.toString());
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
	   
	   JDBCUtil.close(conn, stat);
	return count;
   }
   
   /*	
	 * 搜索查询Byname/企业（营业执照号）
	 */  
	public List<productBean> selcetProductByname(String name){
		 Connection conn=JDBCUtil.getConection();
		   List<productBean> list =new ArrayList<productBean>();
		   StringBuffer sql=new StringBuffer();
		   sql.append("SELECT * FROM product_info WHERE product_name AND status=1 LIKE %"+name+"%");
		   PreparedStatement stmt =null;
		   ResultSet rs=null;
		   try {
			 stmt = conn.prepareStatement(sql.toString());

			 rs=stmt.executeQuery();
			 while(rs.next()){
				 productBean pr=new productBean();
				 pr.setProduct_id(rs.getString(2));
				 pr.setProduct_name(rs.getString(3));
				 pr.setProduct_desc(rs.getString(4));
			     pr.setLicense_id(rs.getString(5));
			     pr.setImage(rs.getString(6));
			     
			     
			     list.add(pr);
			 }
			 
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
	 
		   JDBCUtil.close(conn, stmt, rs);
		return list;
	 
	   }	
	
   /*	
	 * 删除producbyid
	 */  
public int Updelproduct(int product_id){
	 Connection conn=JDBCUtil.getConection();
	StringBuffer sql=new StringBuffer();
	sql.append("UPDATE product_info SET status = 0 WHERE product_id ="+product_id+"");
	PreparedStatement stmt=null;
	int count=0;
	try {
		 stmt=conn.prepareStatement(sql.toString());
		 count=stmt.executeUpdate();
		 
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	JDBCUtil.close(conn, stmt);
	return count;
}	

public int count(String license_id){
	 Connection conn=JDBCUtil.getConection();
	int count=0;
	StringBuffer sql=new StringBuffer();
	sql.append("SELECT COUNT(*) FROM product_info WHERE status = 1 AND license_id=?");
	PreparedStatement st=null;
	ResultSet rs=null;
	try {
		 st = conn.prepareStatement(sql.toString());
		 st.setString(1, license_id);
		 rs = st.executeQuery();
		 
		 while (rs.next()) {			    	
			 count=rs.getInt(1);
			}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  System.out.println("总数据条数"+count);
	  JDBCUtil.close(conn, st, rs);
  return count;	
}


	
	
}
